/*

Copyright (C) 2010  Christoph Schnack 

    This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VocabularyManagement;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import GuiTools.*;
import Mainframe.Mainframe;
import Settings.Language;
import Settings.Settings;
import Storage.Storage;
import Storage.Vocabulary;

class EditLectionsDialog{
	String vte;
	Vocabulary vocToEdit;
	MDTribuitFrame elFrame;
	JComboBox lectCB,sourceLectCB;
	Vector<String> currentLectInverse,currentLect;
	JRadioButton alphSortedRB,lectSortedRB;
	TribuitTextField lectTF;
	Vector<String> sortedKeys = new Vector<String>();
	Storage MAINVOC;
    Settings SET;
    Language LANG;
    Mainframe MAINFRAME;
    MDManager VMDM;
    WordList sourceList,editList;
    
	
	public EditLectionsDialog(String v,Storage mainvoc,Settings set,Mainframe mf,MDManager vmdm){
		MAINVOC=mainvoc;
    	SET=set;
    	LANG=SET.getLanguage();
    	MAINFRAME=mf;
    	VMDM=vmdm;
		vte=v;
		vocToEdit = MAINVOC.getVocabulary(vte);
		elFrame = new MDTribuitFrame(LANG.getEditLections(),VMDM);
		CustomTextArea introTA = new CustomTextArea(LANG.getEditLectsIntro());
		currentLect=new Vector<String>();
		currentLectInverse=new Vector<String>();
		
		//left list
		lectTF = new TribuitTextField(SET,5);
		JLabel ltfLabel = new JLabel(LANG.getNewName()+": ");
		ltfLabel.setOpaque(false);
		String[] lects = vocToEdit.getLections();
		JLabel lectLabel = new JLabel(LANG.getLection()+": ");
		lectLabel.setOpaque(false);
		lectCB = new JComboBox();
		lectCB.setPreferredSize(new Dimension(100,26));
		for(String s : lects){
			if(s.equals("~"))lectCB.addItem(LANG.getUnknown());
			else lectCB.addItem(s);
		}
		lectCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setEditList();
			}
		});
		editList = new EditList();
		JPanel leftTopPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		leftTopPanel.add(lectLabel);
		leftTopPanel.add(lectCB);
		leftTopPanel.add(ltfLabel);
		leftTopPanel.add(lectTF);
		leftTopPanel.setOpaque(false);
		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
		leftPanel.add(leftTopPanel);
		leftPanel.add(editList);
		leftPanel.setBorder(BorderFactory.createTitledBorder(LANG.getEdit()));
		
		//right list
		sourceList = new SourceList();
		sourceLectCB = new JComboBox(vocToEdit.getLections());
		sourceLectCB.setPreferredSize(new Dimension(100,26));
		sourceLectCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setSourceList();
			}
		});
		alphSortedRB = new PJRadioButton(LANG.getAll(),true);
		alphSortedRB.addActionListener(new SourceRBListener());
		lectSortedRB = new PJRadioButton(LANG.getLection());
		lectSortedRB.addActionListener(new SourceRBListener());
		ButtonGroup lBG = new ButtonGroup();
		lBG.add(alphSortedRB);
		lBG.add(lectSortedRB);
		JPanel rightTopPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		rightTopPanel.setOpaque(false);
		rightTopPanel.add(alphSortedRB);
		rightTopPanel.add(lectSortedRB);
		rightTopPanel.add(sourceLectCB);
		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
		rightPanel.add(rightTopPanel);
		rightPanel.add(sourceList);
		rightPanel.setBorder(BorderFactory.createTitledBorder(LANG.getAll()));
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		centerPanel.setBackground(Color.darkGray);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets=new Insets(10,10,10,10);
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.gridwidth=2;
		centerPanel.add(introTA,gbc);
		gbc.fill = GridBagConstraints.NONE;
		gbc.gridwidth=1;
		gbc.gridy=1;
		centerPanel.add(leftPanel,gbc);
		gbc.gridx=1;
		centerPanel.add(rightPanel,gbc);
		
		JButton applyBtn = new JButton(LANG.getOdConfirm());
		applyBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String name = lectTF.getText();
				if(!GuiTools.checkInput(name)){
					JOptionPane.showMessageDialog(elFrame, LANG.getNotThoseSigns()+": "+GuiTools.NOTALLOWEDSIGNS, LANG.getAttention(), JOptionPane.WARNING_MESSAGE);
					lectTF.grabFocus();
					lectTF.selectAll();
					return;
				}
				if(name.equals("")){
					String[] opts = {LANG.getYes(),LANG.getNo()};
					int ans = JOptionPane.showOptionDialog(elFrame,LANG.getWithoutLection(),LANG.getAttention(),JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,opts,null);
					if(ans==JOptionPane.YES_OPTION)name = LANG.getUnknown();
					else return;
				}
				HashMap<String,String[]> hm = vocToEdit.getSectionSortedAndIndexedHM(false);
				for(String s : hm.get((String)lectCB.getSelectedItem())){
					 if(s.contains("-"))vocToEdit.getWordByName(s).setLection(LANG.getUnknown());
				}
				if(currentLect.size()>0){
					String nameTmp;
					for(int i=0;i<currentLect.size();i++){
						nameTmp = (String)currentLect.elementAt(i);
						if(nameTmp.contains("-"))vocToEdit.getWordByName(nameTmp).setLection(name);
					}
				}
				MAINVOC.saveVocabularyIfAutosave(vocToEdit);
				elFrame.dispose();
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
		});
		JButton closeBtn = new JButton(LANG.getClose());
		closeBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				elFrame.dispose();
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(applyBtn);
		buttonPanel.add(closeBtn);
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		elFrame.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getEditLections()));
		elFrame.getContentPane().add(BorderLayout.CENTER,centerPanel);
		elFrame.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
		lectCB.setSelectedIndex(0);
		elFrame.showUpAt(MAINFRAME);
		elFrame.pack();
		elFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
		});
	}
	
	public void setSourceList(){
		if(alphSortedRB.isSelected()) sourceList.setListPanelData(LANG.getAllWords(),GuiTools.sortAndIndexArray(vocToEdit.getWordList(false)),GuiTools.sortAndIndexArray(vocToEdit.getWordList(true)));
		else{
			HashMap<String,String[]> hm = vocToEdit.getSectionSortedAndIndexedHM(false);
			HashMap<String,String[]> hmInv = vocToEdit.getSectionSortedAndIndexedHM(true);
			String lect = (String)sourceLectCB.getSelectedItem();
			sourceList.setListPanelData(LANG.getLection()+": "+lect,hm.get(lect),hmInv.get(lect));
		}
	}
	
	public void setEditList(){
		String str = (String)lectCB.getSelectedItem();
		HashMap<String,String[]> 	hm = vocToEdit.getSectionSortedAndIndexedHM(false),
									hmInv = vocToEdit.getSectionSortedAndIndexedHM(true);
		currentLect.removeAllElements();
		currentLectInverse.removeAllElements();
		for(String s : hm.get(str)){
			currentLect.add(s);
		}
		for(String s : hmInv.get(str)){
			currentLectInverse.add(s);
		}
		editList.setListPanelData(LANG.getLection()+": "+str,currentLect.toArray(new String[]{}),currentLectInverse.toArray(new String[]{}));
		lectTF.setText(str);
	}
	
	class SourceRBListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			setSourceList();
			sourceLectCB.setEnabled(lectSortedRB.isSelected());
		}
	}
	
	class SourceList extends WordList{
		
		public SourceList(){
			super(SET,vocToEdit.getName(),vocToEdit.getNameA(),vocToEdit.getNameB(),GuiTools.sortAndIndexArray(vocToEdit.getWordList(false)),GuiTools.sortAndIndexArray(vocToEdit.getWordList(true)));
		}
		
		public void mouseClick(){
			String ts = sourceList.getSelectedValue().toString();
			if(ts.contains("-")){
	        	 if(!((sourceList.isInverse())?currentLectInverse.contains(ts):currentLect.contains(ts))){
	        		 if(sourceList.isInverse()){
	     	    		currentLectInverse.add(sourceList.getSelectedValue());
	     	    		currentLect.add(vocToEdit.getWordByNameInverse(sourceList.getSelectedValue()).getName());
	     	    	}
	     	    	else{
	     	    		currentLect.add(sourceList.getSelectedValue());
	     	    		currentLectInverse.add(vocToEdit.getWordByName(sourceList.getSelectedValue()).getNameInverse());
	     	    	}
	        	 }
	        	 editList.setListPanelData(LANG.getLection()+": "+(String)lectCB.getSelectedItem(),currentLect.toArray(new String[]{}),currentLectInverse.toArray(new String[]{}));
       	 	}
		}
	}
	
	class EditList extends WordList{
		
		public EditList(){
			super(SET,vocToEdit.getName(),vocToEdit.getNameA(),vocToEdit.getNameB(),vocToEdit.getWordList(false),vocToEdit.getWordList(true));
		}
		
		public void mouseClick(){
			String ts = editList.getSelectedValue().toString();
			if(ts.contains("-")){
				if(editList.isInverse()){
		    		currentLectInverse.remove(editList.getSelectedValue());
		    		currentLect.remove(vocToEdit.getWordByNameInverse(editList.getSelectedValue()).getName());
		    	}
		    	else{
		    		currentLect.remove(editList.getSelectedValue());
		    		currentLectInverse.remove(vocToEdit.getWordByName(editList.getSelectedValue()).getNameInverse());
		    	}
				editList.setListPanelData(LANG.getLection()+": "+(String)lectCB.getSelectedItem(),currentLect.toArray(new String[]{}),currentLectInverse.toArray(new String[]{}));
			}
		}
    	
	}
}